//
//  CQChatMainViewController.m
//  HiChat
//
//  Created by wqiong on 16/1/10.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQChatMainViewController.h"
#import "CQLatestTalkCellTableViewCell.h"
#import "CQRefreshTableViewController.h"

@interface CQChatMainViewController () <IChatManagerDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation CQChatMainViewController


- (void)dealloc {
    [_arr release];
    [_tableV release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    self.arr = [NSMutableArray array];
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    NSString *userName = [userD objectForKey:@"userName"];
    
    if (nil != userName) {
        NSArray *conversations = [[EaseMob sharedInstance].chatManager loadAllConversationsFromDatabaseWithAppend2Chat:YES];
        for (EMConversation *conversation in conversations) {
//            [self.arr addObject:conversation.chatter];
            [self.arr addObject:conversation];
        }
        NSLog(@"%ld",conversations.count);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"对话";
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [self createTable];

}

- (void)createTable {
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    self.tableV.backgroundColor = [UIColor clearColor];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.tableV.frame];
    imageV.image = [UIImage imageNamed:@"cq"];
    self.tableV.backgroundView = imageV;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableV registerClass:[CQLatestTalkCellTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableV];
    [_tableV release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CQLatestTalkCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];
    EMConversation *conversation = [self.arr objectAtIndex:indexPath.row];
    cell.labelUser.text = conversation.chatter;
    id<IEMMessageBody> msgBody = conversation.latestMessage.messageBodies.firstObject;
    if (msgBody.messageBodyType == eMessageBodyType_Text) {
            cell.labelTalk.text = ((EMTextMessageBody *)msgBody).text;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EMConversation *conversation = [self.arr objectAtIndex:indexPath.row];
    CQRefreshTableViewController *chatVC = [[CQRefreshTableViewController alloc]init];
    chatVC.UserTo = conversation.chatter;
    [self.navigationController  pushViewController:chatVC animated:YES];

}


- (void)didReceiveMessage:(EMMessage *)message {
//    EMChatText *txtChat = [[EMChatText alloc] initWithText:@"要发送的消息"];
//    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithChatObject:txtChat];
//    
//    // 生成message
//    EMMessage *qmessage = [[EMMessage alloc] initWithReceiver:@"test2" bodies:@[body]];
//    qmessage.messageType = eMessageTypeChat;
//    [[EaseMob sharedInstance].chatManager asyncSendMessage:qmessage progress:nil];
    NSLog(@"收到消息");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
